// Fill out your copyright notice in the Description page of Project Settings.


#include "Framework/GMSGameInstance.h"
#include "UserWidget/InGameMenuWidget.h"
#include "UserWidget/InGameWidget.h"
#include "Libraries/GameKitFL.h"
#include "Kismet/GameplayStatics.h"
#include "Framework/SlotBasedSaveGame.h"
// #include "Libraries/CommonLibrary.h"


UGMSGameInstance::UGMSGameInstance() {
	DefaultSaveSlotName = TEXT("SaveGame");
	IsBeginPlay = true;
	CurrentGameState = EGameState::BeginPlay;
}

EGameState UGMSGameInstance::PassGameState()
{
	return CurrentGameState;
}

bool UGMSGameInstance::UpdateGameState(const EGameState& DesiredGameState)
{
	if (CurrentGameState == DesiredGameState) {
		return true;
	}
	else {
		CurrentGameState = DesiredGameState;
		return false;
	}
}

void UGMSGameInstance::GetGameWidgets(UWidget*& OutBeginPlayWidget, UWidget*& OutMainMenuWidget, UWidget*& OutPauseMenuWidget, UWidget*& OutLoadingScreenWidget, UInGameWidget*& OutInGameWidget, UInGameMenuWidget*& OutInGameMenuWidget)
{
	OutBeginPlayWidget = BeginPlayWidgte;
	OutInGameMenuWidget = InGameMenuWidget;
	OutInGameWidget = InGameWidget;
	OutLoadingScreenWidget =LoadingScreenWidget;
	OutMainMenuWidget = MainMenuWidget;
	OutPauseMenuWidget = PauseMenuWidget;
}

void UGMSGameInstance::StartNewGame(const FSavedGameplayInfo& GameplayInfo, const FName& LevelName)
{
	return;
}

void UGMSGameInstance::OnAnyKey(FKey& AnyKey)
{
	if (AnyKey.IsGamepadKey()) {

	}
	else {

	}
}

void UGMSGameInstance::Request_Open_Level(const ELevelType& LevelType, const FName& LevelName, const FString& LoadedSlot)
{

}

FString UGMSGameInstance::GetDefaultSlotName()
{
	return DefaultSaveSlotName;
}

bool UGMSGameInstance::SetCurrentSlot(const FString& CurrentSlot)
{
	CurrentSlotName = CurrentSlot;
	return true;
}

FString UGMSGameInstance::GetCurrentSlot()
{
	return CurrentSlotName;
}

bool UGMSGameInstance::UpdatePlayerSaveDataReference(const FSavedPlayerInfo& PlayerInfo)
{
	PlayerInfoToSave = FSavedPlayerInfo(PlayerInfo);
	return SaveCurrentProgress();
}

FSavedPlayerInfo& UGMSGameInstance::GetPlayerSaveDataReference()
{
	return PlayerInfoToSave;
}

bool UGMSGameInstance::SetGameplaySaveDataReference(const FSavedGameplayInfo& GameplayInfo)
{
	GameplayInfoToSave = FSavedGameplayInfo(GameplayInfo);
	return SaveCurrentProgress();
}

FSavedGameplayInfo& UGMSGameInstance::GetGameplaySaveDataReference()
{
	return GameplayInfoToSave;
}

bool UGMSGameInstance::UpdateMissionCSaveReference(FSavedMissionComponent& MissionComponent)
{
	MissionComponentToSave = FSavedMissionComponent(MissionComponent);
	return SaveCurrentProgress();
}

FSavedMissionComponent& UGMSGameInstance::GetMissionCSaveReference()
{
	return MissionComponentToSave;
}

bool UGMSGameInstance::SaveCurrentProgress()
{
	USlotBasedSaveGame* LocSaveGameObject = UGameKitFL::LoadSpecificSlot(CurrentSlotName);
	if (!LocSaveGameObject->IsValidLowLevel()) return false;
	LocSaveGameObject->SavePlayerInfoFun(PlayerInfoToSave);
	UGameKitFL::SaveCurrentGame(LocSaveGameObject, CurrentSlotName);
	return true;
}

bool UGMSGameInstance::LoadSavedGame(const FString& FromSlot)
{
	USlotBasedSaveGame* LocalSaveGameObject=UGameKitFL::LoadSpecificSlot(FromSlot);
	if (!LocalSaveGameObject->IsValidLowLevel()) return false;
	UGameplayStatics::GetPlayerPawn(this, 0)->SetActorTransform(LocalSaveGameObject->SavedPlayerInfo.PlayerTransform);
	UGameplayStatics::GetPlayerController(this, 0)->SetControlRotation(FRotator(LocalSaveGameObject->SavedPlayerInfo.ControlRotation.Pitch, LocalSaveGameObject->SavedPlayerInfo.ControlRotation.Yaw, LocalSaveGameObject->SavedPlayerInfo.ControlRotation.Roll));
	UGameKitFL::SaveCurrentGame(LocalSaveGameObject, FromSlot);
	return true;
}

void UGMSGameInstance::DisplayGameWidget(const EGameState& GameState)
{
	if (GameState == EGameState::InGameMenu) {
		UGameKitFL::GameStateTransition(EGameState::InGameMenu);
		if (!InGameMenuWidget->IsValidLowLevel()) {
			InGameMenuWidget=Cast<UInGameMenuWidget>(CreateWidget(UGameplayStatics::GetPlayerController(GetWorld(), 0), UInGameMenuWidget::StaticClass()));
		}
		InGameMenuWidget->AddToViewport();
		UGameKitFL::SwitchInputMode(InGameMenuWidget, EMouseLockMode::LockAlways, EInputMode::GameAndUI);
	}
	else if (GameState == EGameState::Playing) {
		if (UGameKitFL::GetCurrentGameState() == EGameState::InGameMenu) {
			InGameMenuWidget->RemoveFromParent();
		}
		UGameKitFL::GameStateTransition(EGameState::Playing);
		if (InGameWidget->IsValidLowLevel()) {
			InGameWidget->RemoveFromParent();
		}
		InGameWidget = Cast<UInGameWidget>(CreateWidget(UGameplayStatics::GetPlayerController(GetWorld(), 0), UInGameWidget::StaticClass()));
		InGameWidget->AddToViewport();
		UGameKitFL::SwitchInputMode(InGameWidget, EMouseLockMode::LockAlways, EInputMode::GameOnly);
	}
}

void UGMSGameInstance::HideLoadingScreen(const float& Delay)
{

}

void UGMSGameInstance::TogglePauseMenu()
{

}

void UGMSGameInstance::ToggleInGameMenu(const EInGameMenuType& MenuType)
{

}
